package com.mdc.mansion.common.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.util.CookieGenerator;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 从WEB request/response中获取内容
 */
public class WebUtil {
	
	private static WebUtil webUtil=new WebUtil();

	/**
	 * 获取当前Request
	 * @return
	 */
	private HttpServletRequest getRequest() {  
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();  
        return requestAttributes.getRequest();  
    } 
	
//	/**
//	 * 获取当前response
//	 * @return
//	 */
//	private HttpServletResponse getResponse(){
//		
//		HttpServletResponse resp = ((ServletWebRequest)RequestContextHolder.currentRequestAttributes()).getResponse();
//		return resp;
//	}
	/**
	 * 获取当前Session
	 * @return
	 */
	private HttpSession getSession() {  
        return getRequest().getSession(true);  
    }
	
	/**
	 * 获取访问者IP地址
	 * @return
	 */
	public static String getRemoteIp(){
		HttpServletRequest request=webUtil.getRequest();

        String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
	}
	
	/**
	 * 获取cookie
	 * @param name
	 * @return
	 */
	public static String getCookie(String name){
		Cookie cookie=WebUtils.getCookie(webUtil.getRequest(), name);
		if(cookie!=null){
			return cookie.getValue();
		}
		return null;
	}
	
	/**
	 * 添加cookie
	 * @param cookieName
	 * @param cookieValue
	 * @param cookieMaxAge 单位s
	 * @param cookieDomain
	 * @param cookiePath
	 */
	public static void setCookie(HttpServletResponse response,String cookieName,String cookieValue,int cookieMaxAge,String cookieDomain,String cookiePath){
		CookieGenerator cookieGenerator=new CookieGenerator();
		cookieGenerator.setCookieName(cookieName);
		cookieGenerator.setCookieMaxAge(cookieMaxAge);
		if(StringUtils.isNotEmpty(cookieDomain)){
			cookieGenerator.setCookieDomain(cookieDomain);
		}
		cookieGenerator.setCookiePath(cookiePath);
//		cookieGenerator.addCookie(webUtil.getResponse(), cookieValue);	
		cookieGenerator.addCookie(response, cookieValue);	
		
	}
	
	
	public static void setCookie(HttpServletResponse response,String cookieName,String cookieValue,int cookieMaxAge,String cookiePath){
		setCookie(response,cookieName, cookieValue, cookieMaxAge, null, cookiePath);
	}
	
	public static void setCookie(HttpServletResponse response,String cookieName,String cookieValue,int cookieMaxAge){
		setCookie(response,cookieName, cookieValue, cookieMaxAge, null, "/");
	}
	
	public static void setCookie(HttpServletResponse response,String cookieName,String cookieValue,String cookieDomain,int cookieMaxAge){
		setCookie(response,cookieName, cookieValue, cookieMaxAge, cookieDomain, "/");
	}
	/**
	 * 删除cookie
	 * @param cookieName
	 * @param cookieDomain
	 * @param cookiePath
	 */
	public static void delCookie(HttpServletResponse response,String cookieName,String cookieDomain,String cookiePath){
		CookieGenerator cookieGenerator=new CookieGenerator();
		cookieGenerator.setCookieName(cookieName);
		cookieGenerator.setCookiePath(cookiePath);
		if(StringUtils.isNotEmpty(cookieDomain)){
			cookieGenerator.setCookieDomain(cookieDomain);
		}
//		cookieGenerator.removeCookie(webUtil.getResponse());
		cookieGenerator.removeCookie(response);
	}
	public static void delCookie(HttpServletResponse response,String cookieName,String cookiePath){
		delCookie(response,cookieName, null, cookiePath);
	}
	public static void delCookie(HttpServletResponse response,String cookieName){
		delCookie(response,cookieName, null, "/");
	}
	
	public static String getSessionId() {
		return webUtil.getSession().getId();
	}
	
	public static void putToSession(String key,Object value){
		webUtil.getSession().setAttribute(key, value);
	}
	
	public static void removeSessionAttribute(String key){
		webUtil.getSession().removeAttribute(key);
	}
	public static <T> T getFromSession(String key){
		Object obj = WebUtils.getSessionAttribute(webUtil.getRequest(), key);
		if(obj!=null){
			return (T) obj;
		}
		return null;
	}
}
